﻿// ================================================================================
// The contents of this file are subject to the Mozilla Public License
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/2.0/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
// ================================================================================
using System;
using Reflex.Framework.Collection;
using Reflex.Framework.Communication;
using Reflex.Interface.Aion;

namespace Reflex.Os.Aion.Service
{
	/// <summary>
	/// Represents the ChatConversationService.
	/// </summary>
	class ChatConversationService : ViewModelCommunication
	{
		/// <summary>
		/// Contains the chat type.
		/// </summary>
		private ChatType _eChatType;

		/// <summary>
		/// Contains the instance.
		/// </summary>
		private IAionInstance _hInstance;
		
		/// <summary>
		/// Contains the name.
		/// </summary>
		private string _zName;

		/// <summary>
		/// Initialize a new instance of the ChatConversationService class.
		/// </summary>
		/// <param name="hInstance">Contains the application instance.</param>
		/// <param name="zName">Contains the name.</param>
		public ChatConversationService( IAionInstance hInstance, string zName )
		{
			// Initialize a new instance of the ObservableCollection class.
			Message = new MtObservableCollection<string>();

			// Set the application instance.
			_hInstance = hInstance;

			// Set the name.
			_zName = zName;
		}

		/// <summary>
		/// Initialize a new instance of the ChatConversationService class.
		/// </summary>
		/// <param name="hInstance">Contains the application instance.</param>
		/// <param name="eChatType">Contains the chat type.</param>
		public ChatConversationService( IAionInstance hInstance, ChatType eChatType )
		{
			// Initialize a new instance of the ObservableCollection class.
			Message = new MtObservableCollection<string>();

			// Set the application instance.
			_hInstance = hInstance;

			// Set the chat type.
			_eChatType = eChatType;
		}
		
		/// <summary>
		/// Contains each message.
		/// </summary>
		public MtObservableCollection<string> Message { get; private set; }

		/// <summary>
		/// Contains the message in the queue.
		/// </summary>
		public string Queue
		{
			get
			{
				// Retrieve the property.
				return PropertyGet<string>(() => Queue );
			}
			set
			{
				// Set the property.
				PropertySet(() => Queue, value );
			}
		}

		/// <summary>
		/// Occurs when the send button is invoked.
		/// </summary>
		public Action Send
		{
			get
			{
				// Return the delegate.
				return () =>
				{
					// Check if the queue is available.
					if ( Queue != null && Queue.Length != 0 )
					{
						// Check if this is a private conversation.
						if ( _zName != null ) _hInstance.Controller.Input.Console( string.Format( "/w {0} {1}", _zName, Queue ));
						
						// Otherwise check if this is a normal group conversation.
						else if ( _eChatType == ChatType.Normal ) _hInstance.Controller.Input.Console( string.Format( "/s {0}", Queue ));

						// Otherwise check if this is a shouted group conversation.
						else if ( _eChatType == ChatType.Shout ) _hInstance.Controller.Input.Console( string.Format( "!{0}", Queue ));

						// Otherwise this is a standards-based group conversation.
						else _hInstance.Controller.Input.Console( string.Format( "/{0} {1}", _eChatType.ToString().Substring( 0, 1 ).ToLower(), Queue ));
					}

					// Remove the queue.
					PropertyDefault(() => Queue );
				};
			}
		}
	}
}